import os.path

import numpy as np
import json


class TaskInput:
    def __init__(self,
                 taskId: str,
                 clusterId: str,
                 itemId: str,
                 clusterSize: int,
                 currentIndex: int,
                 timestamp: int,
                 value: float,
                 cpuUsageThresholdAverage: float,
                 cpuUsageThresholdMax: float):
        self.taskId = taskId
        self.clusterId = clusterId
        self.itemId = itemId
        self.clusterSize = clusterSize
        self.currentIndex = currentIndex
        self.data = {
            "timestamp": timestamp,
            "value": value,
        }
        self.thresholdConfig = {
            "cpuUsageThresholdAverage": cpuUsageThresholdAverage,
            "cpuUsageThresholdMax": cpuUsageThresholdMax
        }

    def to_json(self):
        return json.dumps(self.__dict__)

    def save_to_file(self, file_path: str):
        with open(file_path, 'w') as f:
            f.write(self.to_json())


def generate_missing_tasks(taskId: str,
                           clusterId: str,
                           clusterSize: int,
                           save_path: str):
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(clusterSize - 1):
        _ti = TaskInput(taskId=taskId,
                        clusterId=clusterId,
                        itemId=f"item{i}",
                        clusterSize=clusterSize,
                        currentIndex=i,
                        timestamp=1715156387,
                        value=np.random.uniform(low=0, high=0.9),
                        cpuUsageThresholdAverage=0.95,
                        cpuUsageThresholdMax=0.99)
        _ti.save_to_file(file_path=f"{save_path}/{taskId}_missing_task_{i}.json")


def generate_risk_tasks(taskId: str,
                        clusterId: str,
                        clusterSize: int,
                        save_path: str):
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(clusterSize):
        _ti = TaskInput(taskId=taskId,
                        clusterId=clusterId,
                        itemId=f"item{i}",
                        clusterSize=clusterSize,
                        currentIndex=i,
                        timestamp=1715156387,
                        value=np.random.uniform(low=0.95, high=0.99),
                        cpuUsageThresholdAverage=0.95,
                        cpuUsageThresholdMax=0.99)
        _ti.save_to_file(file_path=f"{save_path}/{taskId}_risk_task_{i}.json")


def generate_one_risk_tasks(taskId: str,
                            clusterId: str,
                            clusterSize: int,
                            save_path: str):
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(clusterSize - 1):
        _ti = TaskInput(taskId=taskId,
                        clusterId=clusterId,
                        itemId=f"item{i}",
                        clusterSize=clusterSize,
                        currentIndex=i,
                        timestamp=1715156387,
                        value=np.random.uniform(low=0.55, high=0.69),
                        cpuUsageThresholdAverage=0.95,
                        cpuUsageThresholdMax=0.99)
        _ti.save_to_file(file_path=f"{save_path}/{taskId}_risk_task_{i}.json")

    _ti = TaskInput(taskId=taskId,
                    clusterId=clusterId,
                    itemId=f"item{clusterSize - 1}",
                    clusterSize=clusterSize,
                    currentIndex=clusterSize - 1,
                    timestamp=1715156387,
                    value=np.random.uniform(low=0.99, high=0.9999),
                    cpuUsageThresholdAverage=0.95,
                    cpuUsageThresholdMax=0.99)
    _ti.save_to_file(file_path=f"{save_path}/{taskId}_risk_task_{clusterSize - 1}.json")


def generate_normal_tasks(taskId: str,
                          clusterId: str,
                          clusterSize: int,
                          save_path: str):
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(clusterSize):
        _ti = TaskInput(taskId=taskId,
                        clusterId=clusterId,
                        itemId=f"item{i}",
                        clusterSize=clusterSize,
                        currentIndex=i,
                        timestamp=1715156387,
                        value=np.random.uniform(low=0, high=0.9),
                        cpuUsageThresholdAverage=0.95,
                        cpuUsageThresholdMax=0.99)
        _ti.save_to_file(file_path=f"{save_path}/{taskId}_normal_task_{i}.json")


if __name__ == '__main__':
    generate_normal_tasks(taskId="task1001",
                          clusterId="cluster1001",
                          clusterSize=3,
                          save_path="normal")

    generate_risk_tasks(taskId="task1002",
                        clusterId="cluster1002",
                        clusterSize=4,
                        save_path="risk")

    generate_missing_tasks(taskId="task1003",
                           clusterId="cluster1003",
                           clusterSize=4,
                           save_path="missing")

    generate_one_risk_tasks(taskId="task1004",
                            clusterId="cluster1004",
                            clusterSize=4,
                            save_path="one-risk")
